package com.doctor.windflower_doctor.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.entity.InspectionBeen;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class InspectionListAdapter extends BaseAdapter {
	private Context context;
	private LayoutInflater inflater;
	private List<InspectionBeen>list;
	private DisplayImageOptions options;
	

	public InspectionListAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		list=new ArrayList<InspectionBeen>();
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.loading_img).showImageOnFail(R.drawable.list_default_icon).cacheInMemory(true).cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.ARGB_8888).build();
	}

	@Override
	public int getCount() {
		
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_chacklist, null);
			holder.week_text = (TextView) convertView.findViewById(R.id.week_text);
			holder.date_text = (TextView) convertView.findViewById(R.id.date_text);
			holder.title_text = (TextView) convertView.findViewById(R.id.title_text);
			holder.image = (ImageView) convertView.findViewById(R.id.image);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.week_text.setText(getWeekDate(Long.valueOf(list.get(position).getCreatedAt())));
		holder.date_text.setText(getDayDate(Long.valueOf(list.get(position).getCreatedAt())));
		holder.title_text.setText(list.get(position).getTitle());
		ImageLoader.getInstance().displayImage(list.get(position).getPhotos(), holder.image, options);
		return convertView;
	}

	static class ViewHolder {
		TextView week_text;
		TextView date_text;
		ImageView image;
		TextView title_text;
	}

	public static Bitmap getRoundCornerImage(Bitmap bitmap, int roundPixels) {
		// 创建一个和原始图片一样大小位图
		Bitmap roundConcerImage = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
		// 创建带有位图roundConcerImage的画布
		Canvas canvas = new Canvas(roundConcerImage);
		// 创建画笔
		Paint paint = new Paint();
		// 创建一个和原始图片一样大小的矩形
		Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		RectF rectF = new RectF(rect);
		// 去锯齿
		paint.setAntiAlias(true);
		// 画一个和原始图片一样大小的圆角矩形
		canvas.drawRoundRect(rectF, roundPixels, roundPixels, paint);
		// 设置相交模式
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		// 把图片画到矩形去
		canvas.drawBitmap(bitmap, null, rect, paint);
		return roundConcerImage;
	}

	public void addListData(List<InspectionBeen>list) {
		this.list.addAll(list);
		notifyDataSetChanged();
	}

	public void clearData() {
		
		notifyDataSetChanged();
	}

	@SuppressLint("SimpleDateFormat")
	String getWeekDate(long longstr) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE");
		Date date = new Date(longstr);
		return dateFormat.format(date);
	}

	@SuppressLint("SimpleDateFormat")
	String getDayDate(Long longstr) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日 ");
		Date date = new Date(longstr);
		return dateFormat.format(date);
	}
}
